package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.BomBiz;
import com.yc.sole.erp.request.BomQueryReq;
import com.yc.sole.erp.request.BomReq;
import com.yc.sole.erp.response.BomResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yizuomin
 * @date 2024-01-24
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class BomController {

    private final BomBiz bomBiz;

    /**
     * 新增
     *
     * @param bomReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:save", description = "新增物料清单")
    @PostMapping("/api/erp/bom/saveBom")
    public Result<Void> saveBom(@RequestBody @Validated(SaveGroup.class) BomReq bomReq) {
        bomBiz.saveBom(bomReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param bomReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:update", description = "修改物料清单")
    @PostMapping("/api/erp/bom/updateBomById")
    public Result<Void> updateBomById(@RequestBody @Validated(UpdateGroup.class) BomReq bomReq) {
        bomBiz.updateBomById(bomReq);
        return Result.success();
    }

    /**
     * 通过id批量删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:product:delete", description = "删除物料清单")
    @PostMapping("/api/erp/bom/deleteBom")
    public Result<Void> deleteBom(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        bomBiz.deleteBomByIds(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return Result
     */
    @PreAuth(value = "erp:product:select", description = "查询物料清单")
    @PostMapping("/api/erp/bom/selectBomById/{id}")
    public Result<BomResp> selectBomById(@PathVariable Long id) {
        return Result.data(bomBiz.selectBomById(id));
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:product:select", description = "查询物料清单")
    @PostMapping("/api/erp/bom/selectBomPage")
    public Result<Paging<BomResp>> selectBomPage(@RequestBody @Validated BasePageReq<BomQueryReq> basePageReq) {
        return Result.data(bomBiz.selectBomPage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
